package com.wesley.schedule.support.schedule;

import com.wesley.schedule.domain.entity.ScheduleJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ScheduleManager
 *
 * @author WangPanYong
 * @since 2021/04/13 10:15
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ScheduleManager implements ApplicationRunner {

    private final ScheduleService scheduleService;
    private final ScheduleExecutor scheduleExecutor;
    private final Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info(" ====> ScheduleManager 启动 ...");

        List<ScheduleJob> jobs = scheduleService.findRunningJobs();
        for (ScheduleJob job : jobs) {
            scheduleExecutor.execute(job);
        }
    }

    /**
     * 添加一个Job
     *
     * @author WangPanYong
     * @since 2021/04/14 14:57
     **/
    public void addJob(ScheduleJob job) {
        this.removeJob(job);
        scheduleExecutor.execute(job);
    }

    /**
     * 移除一个Job
     *
     * @author WangPanYong
     * @since 2021/04/14 15:14
     **/
    public void removeJob(ScheduleJob job) {
        JobKey jobKey = JobKey.jobKey(job.getJobId(), job.getJobType());
        String triggerName = ScheduleExecutor.CRON_TRIGGER_PREFIX + job.getJobId();
        String triggerGroup = ScheduleExecutor.CRON_TRIGGER_PREFIX + job.getJobType();
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        try {
            if (scheduler.checkExists(jobKey)) {
                scheduler.deleteJob(jobKey);
            }
            if (scheduler.checkExists(triggerKey)) {
                scheduler.unscheduleJob(triggerKey);
            }
        } catch (SchedulerException e) {
            log.error(" ====> 移除一个Job失败! jobId = {}", job.getJobId());
            log.error(e.getMessage(), e);
        }
    }

    /**
     * removeJob
     * @param name Job名称
     * @param group Job组名称
     * @author WangPanYong
     * @since 2021/04/15 10:20
     **/
    public void removeJob(String name, String group) {
        JobKey jobKey = JobKey.jobKey(name, group);
        TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
        try {
            if (scheduler.checkExists(jobKey)) {
                scheduler.deleteJob(jobKey);
            }
            if (scheduler.checkExists(triggerKey)) {
                scheduler.unscheduleJob(triggerKey);
            }
        } catch (SchedulerException e) {
            log.error(" ====> 移除一个Job失败! jobId = {}", name);
            log.error(e.getMessage(), e);
        }
    }

    /**
     * runJob
     *
     * @author WangPanYong
     * @since 2021/04/15 10:09
     **/
    public void runJob(String jobId) {
        scheduleService.runJob(jobId);
    }



}
